<?php
/**
 * Created by PhpStorm.
 * User: 帅帅吴
 * Date: 2018/10/30
 * Time: 14:21
 * Title:消耗品管理
 */

namespace app\admin\controller;

use app\admin\model\ShopHorn as ShopHornModel;
class Consume extends Index
{
    public function index()
    {
        return view();
    }
    public function indexData()
    {
        $shopHorn = new ShopHornModel();
        $data = $shopHorn->getDataList();
        return json($data);
    }
    public function save()
    {
        $shopHorn = new ShopHornModel();
        $newData = input('post.');
        if(empty($newData)){
            $this->error(lang('add').lang('false'));
        }
        $newData['horn_img'] = '';
        $newData['horn_animation'] = '';
        try{
            $shopHorn->save($newData);
        }catch(\Exception $e){
            $this->error(lang('add').lang('false').$e->getMessage());
        }
        $this->success(lang('add').lang('success'));
    }
    public function edit()
    {
        $shopHorn = new ShopHornModel();
        $newData = input('post.');
        if($newData['type'] == '普通'){
            $newData['type'] = 1;
        }
        if($newData['type'] == '非卖品'){
            $newData['type'] = 2;
        }
        switch($newData['horn_type']){
            case '小喇叭': $newData['horn_type'] = 1;break;
            case '大喇叭': $newData['horn_type'] = 2;break;
            default:$Data['horn_type'] = 3;
        }
        if($newData['is_putaway']=='是'){
            $newData['is_putaway']=0;
        }
        if($newData['is_putaway']=='否'){
            $newData['is_putaway']=1;
        }
        try{
            $shopHorn->save($newData,['horn_id' => input('horn_id')]);
        }catch(\Exception $e){
            $this->error(lang('edit').lang('false').$e->getMessage());
        }
        $this->success(lang('edit').lang('success'));
    }
}